package com.sikao.app.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.GetSmsSubmitBean;
import com.sikao.app.bean.RetrievePayPswSubmitBean;
import com.sikao.app.dao.RetrievePayPswModel;
import com.sikao.app.dao.SmsCodeModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.StringUtils;
import com.sikao.app.utils.TimeCountUtil;
/**
 * 找回支付密码
 */
public class RetrievePayPswActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;

	private EditText phoneNumberEt;
	private EditText smsCodeEt;
	private Button sendCodeBtn;
	private EditText newPswEt1;
	private EditText newPswEt2;
	private Button changePswBtn;
	private GetSmsSubmitBean bean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_retrieve_pay_psw);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {

	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		phoneNumberEt = (EditText) findViewById(R.id.phone_number_et);
		smsCodeEt = (EditText) findViewById(R.id.sms_code_et);
		sendCodeBtn = (Button) findViewById(R.id.send_code_btn);
		newPswEt1 = (EditText) findViewById(R.id.new_psw_et1);
		newPswEt2 = (EditText) findViewById(R.id.new_psw_et2);
		changePswBtn = (Button) findViewById(R.id.change_psw_btn);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		sendCodeBtn.setOnClickListener(this);
		changePswBtn.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("找回支付密码");
		backIv.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.send_code_btn:
				getCode();
				break;
			case R.id.change_psw_btn:
				changePayPsw();
				break;
			default:
				break;
		}
	}

	private void getCode() {
		if (TextUtils.isEmpty(phoneNumberEt.getText().toString())) {
			Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
			return;
		}
		/*if (!StringUtils.isMobile(phoneNumberEt.getText().toString())) {
			Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
			return;
		}*/
		bean = new GetSmsSubmitBean();
		bean.setType("forgetPayPwd");
		bean.setPhone(phoneNumberEt.getText().toString());
		bean.setId("1");

		new SmsCodeModel(this, new OnNetBackListener<Bean>() {
			@Override
			public void netBack(Bean smsBean) {
				if (smsBean.getStatus().equals("1")) {
					Toast.makeText(RetrievePayPswActivity.this, "成功发送", Toast.LENGTH_SHORT).show();
					TimeCountUtil timeCountUtil = new TimeCountUtil(60000, 1000, sendCodeBtn);
					timeCountUtil.start();
				} else {
					Toast.makeText(RetrievePayPswActivity.this, smsBean.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		}).request(JSONUtil.toJSON(bean), true);
	}

	private void changePayPsw() {

		if (!checkInput()) {
			return;
		}
		RetrievePayPswSubmitBean bean = new RetrievePayPswSubmitBean();
		bean.setPhone(phoneNumberEt.getText().toString());
		bean.setPhoneCode(smsCodeEt.getText().toString());
		bean.setPayPwd(newPswEt1.getText().toString());
		bean.setAgapwd(newPswEt2.getText().toString());
		bean.setType("forgetPayPwd");
		bean.setId("1");
		new RetrievePayPswModel(this, new OnNetBackListener<Bean>() {
			@Override
			public void netBack(Bean data) {
				if (data == null){
					return;
				}
				if (data.getStatus().equals("1")){
					Toast.makeText(RetrievePayPswActivity.this, "成功设置支付密码", Toast.LENGTH_SHORT).show();
					finish();
				}else{
					Toast.makeText(RetrievePayPswActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		}).request(JSONUtil.toJSON(bean), true);
	}

	private boolean checkInput() {
		if (TextUtils.isEmpty(phoneNumberEt.getText().toString())) {
			Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
			return false;
		}
		/*if (!StringUtils.isMobile(phoneNumberEt.getText().toString())) {
			Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
			return false;
		}*/
		if (TextUtils.isEmpty(smsCodeEt.getText().toString())) {
			Toast.makeText(this, "请输入短信验证码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (newPswEt1.getText().toString().length() != 6) {
			Toast.makeText(this, "密码不符合要求，请输入六位纯数字密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (TextUtils.isEmpty(newPswEt2.getText().toString())) {
			Toast.makeText(this, "请再次输入新密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!(newPswEt1.getText().toString()).equals(newPswEt2.getText().toString())) {
			Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
}
